package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.*;
import com.whz.workmanageol.entity.*;
import com.whz.workmanageol.service.StudentService;
import com.whz.workmanageol.utils.FileDownload;
import com.whz.workmanageol.utils.FileUpload;
import com.whz.workmanageol.vo.CourseVO;
import com.whz.workmanageol.vo.PublishWorkVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private PublishMapper publishMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private FileUpload fileUpload;
    @Autowired
    private FileDownload fileDownload;

    @Override
    public List<CourseVO> findCoure(String studentid) {
        String gradeid = studentMapper.selectByPrimaryKey(studentid).getGradeid();
        List<Course> courseList = courseMapper.findCourseByGradeid(gradeid);
        List<CourseVO> courseVOList = new ArrayList<>();
        CourseVO courseVO = null;
        for(Course cou : courseList){
            courseVO = new CourseVO(cou.getId(), cou.getCourseid(), cou.getCoursename(), gradeid);
            courseVOList.add(courseVO);
        }
        Collections.sort(courseVOList, Comparator.comparing(CourseVO::getCoursename));
        return courseVOList;
    }

    @Override
    public List<PublishWorkVO> findPublish(String studentid, String courseid, String gradeid) {
        List<PublishWorkVO> publishWorkVOList = new ArrayList<>();
        Example example = new Example(Publish.class);
        example.createCriteria()
                .andEqualTo("gradeid", gradeid)
                .andEqualTo("courseid", courseid);
        List<Publish> publishList = publishMapper.selectByExample(example);
        for(Publish pub : publishList){
            PublishWorkVO publishWorkVO = null;

            Example exa = new Example(Work.class);
            exa.createCriteria()
                    .andEqualTo("publishid", pub.getId())
                    .andEqualTo("studentid", studentid);
            List<Work> workList = workMapper.selectByExample(exa);
            Work theWork = workList.get(0);
            if(!Objects.isNull(theWork)){
                publishWorkVO = new PublishWorkVO(pub, theWork.getId());
            }
            publishWorkVOList.add(publishWorkVO);
        }
//        Collections.sort(publishWorkVOList, PublishWorkVO::getPublish:);
        Collections.sort(publishWorkVOList, new Comparator<PublishWorkVO>() {
            @Override
            public int compare(PublishWorkVO o1, PublishWorkVO o2) {
                return o1.getPublish().getSeq().compareTo(o2.getPublish().getSeq());
            }
        });
        return publishWorkVOList;
    }

    @Override
    public void replaceWork(MultipartFile file, String workid) {
        fileUpload.replaceWork(file, workid);
    }

    @Override
    public void saveWorkSubmitDate(String workid) {
        Work work = workMapper.selectByPrimaryKey(workid);
        work.setStudentsubdate(new Date());
        workMapper.updateByPrimaryKey(work);
    }

    @Override
    public boolean couldSubmit(String publishid) {
        Date deadline = publishMapper.selectByPrimaryKey(publishid).getDeadline();
        if(deadline.after(new Date())){
            return true;
        }
        return false;
    }

    @Override
    public Publish findPublishById(String publishid) {
        return publishMapper.selectByPrimaryKey(publishid);
    }

    @Override
    public String downAttach(String publishid, HttpServletResponse response) {
        return fileDownload.downAttach(publishid, response);
    }

    @Override
    public String queryWorkScore(String workid) {
        String score = workMapper.queryScoreById(workid);
        logger.info("score: {}", score);
        return workMapper.queryScoreById(workid);
    }


}
